package com.liruo.demo.serialize.jackson.config;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liruo.demo.serialize.jackson.model.User;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author:liruo
 * @Date:2023-02-20-02:29:38
 * @Desc
 */
@Slf4j
public class StrUserDeserializer extends JsonDeserializer<User> {

  @Override
  public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
      throws IOException, JacksonException {
    JavaType contextualType = deserializationContext.getContextualType();
    log.info("{}", contextualType);

    ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
    JsonNode root = mapper.readTree(jsonParser);
    String name = root.get("name").asText();

    if (name.startsWith("=大傻逼=")) {
      name = name.substring("=大傻逼=".length());
    }

    String des = root.get("des").asText();
    if (des.startsWith("=大傻逼=")) {
      des = des.substring("=大傻逼=".length());
    }

    return new User(name, des);
  }

  @Override
  public Class<User> handledType() {
    return User.class;
  }
}
